|||##########################################################################
|||## A Macroquest2 Casino written by Grimshad & Killians
|||## casinoblackjack.mac - Handles all blackjack code
|||##########################################################################
|||---------------------------------------------------------------------------------------------------------BlackJack Events
#event Playblackjack "#1# says, 'Play Blackjack'"
#event Hold "#1# says, 'Hold'"
#event Blackjack "#1# #*# 'Blackjack'"
|||---------------------------------------------------------------------------------------------------------BlackJack Initialization
Sub BJInit
	/if (${Debug}) /echo in BJInit
	/declare DealerTotal int outer 0
	/declare DealerRollCount int outer 0
	/declare DealerAces int outer 0
	/declare PlayersInGame int outer 0
	/declare BJTableArray[10] string outer 0
	/declare BJState int outer 0
	||| BJState 0=none 1=betting 2=playing
/return
|||---------------------------------------------------------------------------------------------------------BlackJack Info
Sub Event_Blackjack(Line, Player)
	/if (${Debug}) /echo in Blackjack(${player})
	/varset Player ${Player.Right[-2].Left[-1]}
	/delay ${TellDelay}
	/tell ${Player} In this game, you will roll 10, each number is equal to its value, 0 is a jack, and 1 is ace(1 or 11). If you get 1+0 or 0+1 then you instantly win. You can roll as many times as you want. If you go over 21 you bust and lose. If you [hold] before going over 21 the casino gets to roll the same way. If the casino busts you win double your money. If the casino matches or beats your total you lose. [Play Blackjack]?
/return
|||---------------------------------------------------------------------------------------------------------BlackJack
Sub Event_Playblackjack(Line, Player)
	/if (${Debug}) /echo in Playblackjack(${Player})
	/varset Player ${Player.Right[-2].Left[-1]}
	|||Make sure the player isn't already playing blackjack
	/if (${${Player}[2]} == 3) /return
	|||Return if player has no money
	/if (!${Defined[${Player}]}) {
		/call BankedCheck ${Player}
		/if (!${Macro.Return}) /return
	}
	|||If player is still at blackjack or roulette table, don't let them switch games.
	/if (${${Player}[2]} == 3 || ${${Player}[2]} == 4) {
		/say ${Player}, Please wait until you are removed from your current table before starting a new game.
		/return
	}
	|||For now, don't let players join the table till none or betting phase.
	/if (${BJState} != 0 && ${BJState} != 1) {
		/say ${Player}, You cannot join the Blackjack table during play. Please wait until the betting phase.
		/return
	}
	|||Fill the table
	/declare i int local
	/for i 1 to 11
		/if (${i} == 11) {
			/say ${Player}, The blackjack table is currently full, please play a different game or wait for a seat to open up.
			/return
		}
		/if (${BJTableArray[${i}].Equal[0]}) {
			/varset BJTableArray[${i}] ${Player}
			/goto :continueplayblackjack
		}
	/next i
	
:continueplayblackjack
	/call SetPlayerStatus ${Player} NONE 3 NONE
	/say ${Player} Has joined the BlackJack table. Please wait for BlackJack betting to open before placing your bet.
	/if (!${Defined[${BJTimer}]} && !${BJState}) {
	/declare BJTimer timer outer 5s
	}
/return

Sub Event_Hold(Line, Player)
	/if (${Debug}) /echo in Hold(${Player})
	/varset Player ${Player.Right[-2].Left[-1]}
	|||Make sure player is playing blackjack and hasn't busted yet.
	/if (${${Player}[2]} != 3) /return
	/if (${${Player}[7]} < 2) {
	/say ${Player}, you have to roll at least twice to hold.
	/return
	}
	|||Set held to true for this player
	/if (!${${Player}[6]}) {
		/varset ${Player}[8] 1
		/say ${Player} Holds with ${${Player}[4].Left[-7]}.
	}
	|||If there is still someone at the table who has not held, don't start casino rolls
	/declare i int local
	/for i 1 to 10
		/if (${BJTableArray[${i}].NotEqual[0]}) {
			/if (!${${BJTableArray[${i}]}[8]}) /return
		}
	/next i
	|||Start Casino Blackjack Rolls
	/varset BJState 2
	/delay ${TellDelay}
	/ran 10
/return

Sub BlackjackCheck
	/if (${Debug}) /echo in BlackjackCheck
	/if (${BJState} == 0) {
		|||Move to betting state
		/say BlackJack betting is now open for 15 seconds, please [Bet #] now.
		/varset BJState 1
		/varset BJTimer 15s
	} else /if (${BJState} == 1) {
		|||Remove people who didn't bet from the table
		/declare i int local
		/declare playercount int local 0
		/declare playersleft int local 0
		/for i 1 to 10
			/if (${BJTableArray[${i}].NotEqual[0]}) {
				/varcalc playercount ${playercount} + 1
				/varset ${BJTableArray[${i}]}[8] 0
				/if (!${${BJTableArray[${i}]}[3]}) {
					/varcalc playersleft ${playersleft} + 1
					/say ${BJTableArray[${i}]} Has left the BlackJack table.
					/varset ${BJTableArray[${i}]}[2] 0
					/varset BJTableArray[${i}] 0
				}
			}
		/next i
		/varcalc PlayersInGame ${playercount} - ${playersleft}
		|||Reset if noone is playing
		/if (${playercount} == ${playersleft}) {
			/deletevar BJTimer
			/varset BJState 0
			/return
		}
		|||Move to playing state
		/say BlackJack betting is now closed, please /ran 10 until you want to [Hold].
		/varset BJState 2
		/deletevar BJTimer
	}
/return

Sub CasinoBlackJack(Player, int RollMin, int RollMax, int Roll)
	/if (${Debug}) /echo in CasinoBlackJack(${Player}, RollMin ${RollMin}, RollMax ${RollMax}, Roll ${Roll})
	/declare i int local
	|||Add Roll Total
	/if (${Roll} == 0) /varset Roll 10
	/varcalc DealerTotal ${DealerTotal} + ${Roll}
	|||Handle BlackJack
	/if (${DealerRollCount} == 2 && ${DealerTotal} == 1) {
		/for i 1 to 10
			/if (${BJTableArray[${i}].NotEqual[0]} && !${${BJTableArray[${i}]}[6]}) {
				/call PlayerLoses ${BJTableArray[${i}]} ${${BJTableArray[${i}]}[3]}
				/say ${BJTableArray[${i}]} has lost. -${${BJTableArray[${i}]}[3]}
				/call ResetPlayerStatus ${${BJTableArray[${i}]} ${${BJTableArray[${i}]}[2]}
			}
		/next i
			
		/say Dealer wins with BlackJack.
		/varset BJState 0
		/declare BJTimer timer outer 5s
		/return
	}
	|||Handle Aces
	/if (${Roll} == 1) {
		/if (${Math.Calc[${DealerTotal} + 11]}  < 22) {
			/varcalc DealerAces ${DealerAces} + 1
			/varcalc DealerTotal ${DealerTotal} + 10
		}
	}
	|||Handle Hits
	/if (${DealerTotal} < 17) {
	/delay 3
	/ran 10
	}
	||||||Handle Busts and Ace Calculations
	/if (${DealerTotal} > 21) {
		|||Handle Ace calc
		/if (${DealerAces} > 0) {
			/for i 1 to ${DealerAces}
				/if (${DealerTotal} > 21 && !${DealerAces}) {
					/varcalc ${DealerTotal} ${DealerTotal} - 10
					/varcalc ${DealerAces} ${DealerAces} - 1
				} else {
					/goto :leavecasinoaceloop
				}
			/next i
		}
	:leavecasinoaceloop
		|||Handle Bust
		/if (${DealerTotal} > 21) {
			/say Dealer busts with ${DealerTotal}.
			/for i 1 to 10
				/if (${BJTableArray[${i}].NotEqual[0]} && !${${BJTableArray[${i}]}[6]}) {
					/call PlayerWins ${BJTableArray[${i}]} ${${BJTableArray[${i}]}[3]}
					/say ${BJTableArray[${i}]} has won. +${${BJTableArray[${i}]}[3]}
					/call ResetPlayerStatus ${BJTableArray[${i}]} ${${BJTableArray[${i}]}[2]}
				}
			/next i	
		}
		/varset BJState 0
		/declare BJTimer timer outer 5s
		/return
	}
	|||Handle wins/losses
	/if (${DealerTotal} >= 17 && ${DealerTotal} < 22) {
		/say Dealer has ${DealerTotal}.
		/for i 1 to 10
			/if (${BJTableArray[${i}].NotEqual[0]} && !${${BJTableArray[${i}]}[6]} && ${${BJTableArray[${i}]}[3]}) {
				/if (${${BJTableArray[${i}]}[4]} < ${DealerTotal}) {
					/call PlayerLoses ${BJTableArray[${i}]} ${${BJTableArray[${i}]}[3]}
					/say ${BJTableArray[${i}]} has lost. -${${BJTableArray[${i}]}[3]}
					/call ResetPlayerStatus ${BJTableArray[${i}]} ${${BJTableArray[${i}]}[2]}
				} else /if (${${BJTableArray[${i}]}[4]} > ${DealerTotal}) {
					/call PlayerWins ${BJTableArray[${i}]} ${${BJTableArray[${i}]}[3]}
					/say ${BJTableArray[${i}]} has won. +${${BJTableArray[${i}]}[3]}
					/call ResetPlayerStatus ${BJTableArray[${i}]} ${${BJTableArray[${i}]}[2]}
				} else /if (${${BJTableArray[${i}]}[4]} == ${DealerTotal}) {
					/say ${BJTableArray[${i}]} has tied. -/+
					/call ResetPlayerStatus ${BJTableArray[${i}]} ${${BJTableArray[${i}]}[2]}
				}
			}
		/next i
		/varset BJState 0
		/declare BJTimer timer outer 5s
		/return
	}
	/varcalc DealerRollCount ${DealerRollCount}+1
/return

Sub PlayerBlackJack(Player, int RollMin, int RollMax, int Roll)
	/if (${Debug}) /echo in PlayerBlackJack(${Player}, RollMin ${RollMin}, RollMax ${RollMax}, Roll ${Roll})
	/declare i int local
	|||Handle No Bet
	/if (${${Player}[3]} == 0) {
		/say ${Player}, You need to [Bet #] before you can roll.
		/return
	}
	|||No Rolls until betting is over
	/if (${BJState} == 1) {
		/say ${Player}, Please wait until betting is closed to roll.
		/return
	}
	|||Handle Bad Rolls
	/if (!${RollMin} && ${RollMax} != 10) {
		/say ${Player}, Your roll was incorrect, please /ran 10.
		/return
	}
	|||Handle Aces
	/if (${Roll} == 1) {
		/if (${Math.Calc[${${Player}[4]} + 11]} < 22) {
			/varcalc ${Player}[5] ${${Player}[5]} + 1
			/varset Roll 11
		}
	}
	|||Add Roll to Total
	/if (${Roll} == 0) /varset Roll 10
	/varcalc ${Player}[4] ${${Player}[4]} + ${Roll}
	|||Handle BlackJack
	/if (${${Player}[7]} == 2 && ${${Player}[4]} == 1) {
		/call PlayerWins ${Player} ${${Player}[3]}
		/say ${Player} wins with BlackJack. +${${Player}[3]}
		/call ResetPlayerStatus ${Player} ${${Player}[2]}
		/varset ${Player}[8] 1
		/varcalc PlayersInGame ${PlayersInGame} - 1
		/if (!${PlayersInGame}) {
			/varset BJState 0
			/declare BJTimer timer outer 5s
		}
		/return
	}
	|||Handle Busts and Ace Calculations
	/if (${${Player}[4]} > 21) {
		|||Handle Ace calc
		/if (${${Player}[5]} > 0) {
			/for i 1 to ${${Player}[5]}
				/if (${${Player}[4]} > 21 && ${${Player}[5]} != 0 ) {
					/varcalc ${Player}[4] ${${Player}[4]} - 10
					/varcalc ${Player}[5] ${${Player}[5]} - 1
				} else {
					/goto :leaveaceloop
				}
			/next i
		}
		
	:leaveaceloop
		|||Handle Bust
		/if (${${Player}[4]} > 21) {
			/varset ${Player}[6] 1
			/call PlayerLoses ${Player} ${${Player}[3]}
			/say ${Player} busted with ${${Player}[4].Left[-7]}. -${${Player}[3]}
			/call ResetPlayerStatus ${Player} ${${Player}[2]}
			/varset ${Player}[8] 1
			/varcalc PlayersInGame ${PlayersInGame} - 1
		/if (!${PlayersInGame}) {
			/varset BJState 0
			/declare BJTimer timer outer 5s
		}
			/return
		}
	}
	/say ${Player} has ${${Player}[4].Left[-7]}.
	/varcalc ${Player}[7] ${${Player}[7]} + 1
/return
